package com.kehutong.charge.service.impl;

import com.kehutong.charge.entity.MeterClassify;
import com.kehutong.charge.entity.MeterManage;
import com.kehutong.charge.entity.MeterTypeConfig;
import com.kehutong.charge.service.MeterClassifyService;
import com.kehutong.common.errcode.Result;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.Page;
import org.coraframework.orm.jdbc.execute.entity.FindPage;
import org.coraframework.util.Objects;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @program: kht
 * @description: MeterClassifyServiceImpl
 * @author: joneron
 * @create: 2021/6/21
 **/
public class MeterClassifyServiceImpl implements MeterClassifyService {

    private static final Logger logger = LoggerFactory.getLogger(MeterClassifyServiceImpl.class);

    @Inject
    private JdbcSession jdbcSession;




    @Override
    public Page<MeterClassify> findPage(JSONObject jsonObject) {
        FindPage<MeterClassify> find = jdbcSession.findPage(MeterClassify.class)
                .eq("deleted", false)
                .like(jsonObject,"typeName")
                .eq(jsonObject,"meterTypeNo")
                .order("sort", "desc")
                .order("createTime", "desc");
        Page<MeterClassify> page = find.page(jsonObject).exe();

        List<MeterClassify> list = page.getList();
        if(Objects.isEmpty(list)){
            return page;
        }

        Set<String> typeNos = list.stream().map(MeterClassify::getMeterTypeNo).collect(Collectors.toSet());
        List<MeterManage> dbTypeNoList = jdbcSession.findArray(MeterManage.class)
                .eq("deleted", false)
                .in("meterTypeNo", typeNos)
                .exe();
        if(Objects.isEmpty(dbTypeNoList)){
            return page;
        }

        list.stream().map(l->{ long count = dbTypeNoList.stream().filter(d -> d.getMeterTypeNo().equals(l.getMeterTypeNo())).count();
            l.setNumber((int) count);return l; }).collect(Collectors.toList());

        return page;
    }


    @Override
    public List<MeterClassify> findList(JSONObject jsonObject) {
        return jdbcSession.findArray(MeterClassify.class)
                .eq("deleted", false)
                .eq(jsonObject,"meterTypeNo")
                .exe();
    }

    @Override
    public Object save(MeterClassify meterClassify) {

        String name = meterClassify.getTypeName();
        List<MeterClassify> listMt = jdbcSession.findArray(MeterClassify.class)
                .eq("deleted", false)
                .eq("typeName",name)
                .eq("meterTypeNo",meterClassify.getMeterTypeNo()).exe();

        if(listMt.size() > 0){
            return Result.error(2021, "类型名不能重复！");
        }

        MeterTypeConfig meterTypeConfig = jdbcSession.findById(MeterTypeConfig.class,meterClassify.getMeterTypeNo());
        meterTypeConfig.setNumber(meterTypeConfig.getNumber()+1);
        jdbcSession.updateById(meterTypeConfig);

        meterClassify.setMeterTypeName(meterTypeConfig.getTypeName());
        return jdbcSession.insert(meterClassify);
    }

    @Override
    public Object update(MeterClassify meterClassify) {
        return jdbcSession.updateById(meterClassify);
    }

    @Override
    public Object deleteById(String id) {
        MeterClassify result = Pool.get(MeterClassify.class, id);
        if (result != null) {
            result.setDeleted(true);
            jdbcSession.updateById(result);
        }
        return 1;
    }

    @Override
    public Object get(MeterClassify entity) {
        return Result.success(entity);
    }
}
